<template>
  <view class="fridgefreezer">
    <SimpleControl :control="control" hide-offline-tips-forced>
      <FridgeSetting :control="control" @open-setting="onOpenSettingModal" @open-modal="onOpenSettingDialog" />
      <FridgeSettingModal v-model:show="showSettingModal" :control="control" />
      <FridgeSettingDialog
        :show="showSettingDialg"
        :control="control"
        @close="closeFridgeSetting"
      ></FridgeSettingDialog>
      <LocalFlexSetting :control="control" />
    </SimpleControl>

    <!-- 固件升级成功 -->
    <SuccessAcknowledge v-if="otaHelper" :helper="otaHelper" />
  </view>
</template>

<script setup lang="ts">
import { useScrollProvider } from '@/utils/composition/useScroll'
// 注意：请勿将onShareAppMessage删除
import { onLoad, onPageScroll, onShareAppMessage } from '@dcloudio/uni-app'
import SimpleControl from '@/pages/appliance/view/control/simpleControl.vue'
import LocalFlexSetting from './LocalFlexSetting.vue'
import FridgeSetting from './FridgeSetting.vue'
import FridgeSettingModal from './FridgeSettingModal.vue'
import { useI18n } from 'vue-i18n'
import type { FridgeFreezerController, FridgeSettingGroup } from '@/controller/FridgeFreezer/definition'
import { showLoading } from '@/utils/mpHelper'
import FridgeSettingDialog from './FridgeSettingDialog.vue'
import { useInhouseOtaHelper } from '../view/useOtaHelper'
import SuccessAcknowledge from '../settings/ota/modals/successAcknowledge.vue'

const showSettingModal = ref(false)
const showSettingDialg = ref(false)
const i18n = useI18n()
const { setScrollTop } = useScrollProvider()
onPageScroll(({ scrollTop }) => setScrollTop(scrollTop))

const props = defineProps<{
  control: FridgeFreezerController.Control
}>()
const control = props.control

onLoad((query: any) => {
  if (query?.flexModeId) {
    control.store.flexModeId = query?.flexModeId
    control.store.flexModeName = decodeURIComponent(decodeURIComponent(query?.flexModeName))
  }
})

/**
 * OTA升级
 */
const otaHelper = useInhouseOtaHelper(control)

/**
 * 打开设置弹框
 */
async function onOpenSettingModal(settingGroup: FridgeSettingGroup) {
  //离线屏蔽处理

  if (control.store.isVirtualAppliance) {
    control.store.loadDemoMenuSettings?.(settingGroup.key)
  } else {
    await showLoading(() => control.store.loadMenuSettings(settingGroup.key))
  }
  showSettingModal.value = true
}
async function onOpenSettingDialog(settingGroup: FridgeSettingGroup) {
  // 离线屏蔽处理
  await showLoading(() => control.store.loadMenuSettings(settingGroup.key))
  showSettingDialg.value = true
}
function closeFridgeSetting() {
  showSettingDialg.value = false
}
</script>
